package com.example.viewpagertest2;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by Michael on 2015/1/13.
 * 一般用这个比较好
 */
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {


    private ArrayList<String> tabList;

    private ArrayList<Fragment> fragmentList;

    public ViewPagerFragmentAdapter(FragmentManager fm,ArrayList<Fragment> fragmentList,ArrayList<String> tabList){
        super(fm);

        this.fragmentList = fragmentList;

        this.tabList = tabList;
    }

    @Override
    public Fragment getItem(int i) {
        int newPosition;
        if(i>=0){
            newPosition = (i+50)%5;
        }else {
            newPosition =(-i)%5;
        }
        switch (newPosition){
            case 0:
                return fragmentList.get(0);
            case 1:
                return fragmentList.get(1);
            case 2:
                return fragmentList.get(2);
            case 3:
                return fragmentList.get(3);
            case 4:
                return fragmentList.get(4);
        }
        return null;
    }

    @Override
    public int getCount() {
//        return fragmentList.size();
        return 5000;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        int newPosition;
        if(position>=0){
            newPosition = (position+50)%5;
        }else {
            newPosition =(-position)%5;
        }
        switch (newPosition){
            case 0:
                return tabList.get(0);
            case 1:
                return tabList.get(1);
            case 2:
                return tabList.get(2);
            case 3:
                return tabList.get(3);
            case 4:
                return tabList.get(4);
        }
        return null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }
}
